package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.PaymentTypeDAO;
import com.mbc.rmt.dao.PaymentTypeDetailDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.PaymentTypeDataList;
import com.mbc.rmt.entities.PaymentType;
import com.mbc.rmt.entities.PaymentTypeDetail;
import com.mbc.rmt.entities.PaymentTypeRpt;


public class PaymentTypeMgr extends AbstractMgr {

	private PaymentTypeDAO paymentTypeDAO;
	
	public PaymentTypeMgr(){}
	
	public PaymentTypeMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public void create(PaymentType entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			entity.setPaymentTypeId(KeyGenerator.generateKey(getConnection()));
			paymentTypeDAO.create(entity);
			
			PaymentTypeDetailDAO l_PaymentTypeDetailDAO = RemittanceDAOFactory.getPaymentTypeDetailDAO(getConnection());
			
			for (PaymentTypeDetail l_detail : entity.getPaymentTypeDetailList()) {
				l_detail.setPaymentTypeId(entity.getPaymentTypeId());
				l_detail.setPaymentTypeDetailId(KeyGenerator.generateKey(getConnection()));
				l_PaymentTypeDetailDAO.create(l_detail);
			}
			
			commintTransaction();
		} catch (SQLException e) {
			rollbackTransaction();
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	public void createDetail(PaymentTypeDetail entity) throws Exception {
		openConnection();
		try {
			PaymentTypeDetailDAO l_PaymentTypeDetail = RemittanceDAOFactory
					.getPaymentTypeDetailDAO(getConnection());
			entity.setPaymentTypeDetailId(KeyGenerator.generateKey(getConnection()));
			l_PaymentTypeDetail.create(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void update(PaymentType entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			paymentTypeDAO.update(entity);
			
			PaymentTypeDetailDAO l_PaymentTypeDetailDAO = RemittanceDAOFactory.getPaymentTypeDetailDAO(getConnection());
			l_PaymentTypeDetailDAO.deleteByPaymentTypeId(entity.getPaymentTypeId());
			for (PaymentTypeDetail l_detail : entity.getPaymentTypeDetailList()) {
				l_detail.setPaymentTypeId(entity.getPaymentTypeId());
				l_detail.setPaymentTypeDetailId(KeyGenerator.generateKey(getConnection()));
				l_PaymentTypeDetailDAO.create(l_detail);
			}
			
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long key) throws Exception {
		openConnection();
		try {
			beginTransaction();
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			PaymentTypeDetailDAO l_PaymentTypeDetailDAO = RemittanceDAOFactory.getPaymentTypeDetailDAO(getConnection());
			l_PaymentTypeDetailDAO.deleteByPaymentTypeId(key);
			paymentTypeDAO.delete(key);
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public PaymentType find(long key) throws Exception {
		openConnection();
		try {
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			PaymentType l_PaymentType = paymentTypeDAO.find(key);
			PaymentTypeDetailDAO l_PaymentTypeDetailDAO = RemittanceDAOFactory.getPaymentTypeDetailDAO(getConnection());
			l_PaymentType.setPaymentTypeDetailList(l_PaymentTypeDetailDAO.findByPaymentTypeId(key));
			return l_PaymentType;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<PaymentType> findAll() throws Exception {
		openConnection();
		try {
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			return paymentTypeDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public PaymentTypeDataList findPaymentTypes(CriteriaParams cri)throws Exception{
		openConnection();
		try {
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			return paymentTypeDAO.findPaymentTypes(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public PaymentType findPaymentTypeByCode(String code)throws Exception{
		openConnection();
		try {
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(dbManager.getConnection());
			return paymentTypeDAO.findPaymentTypeByCode(code);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	//MBC Begin
	// For Report
	public JasperPrint preparePaymentTypeReport(PaymentTypeRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(paymentTypeDAO.preparePaymentTypeReport(info));
			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/PaymentType.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	public PaymentType getPaymentType(PaymentType l_info) throws Exception {
		openConnection();
		try{
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(dbManager
					.getConnection());
			String code=l_info.getCode();
			return paymentTypeDAO.getPaymentType(code);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public ArrayList<PaymentTypeDetail> findPaymentTypeDetails(long paymentTypeId)
			throws Exception {
		openConnection();
		try {
			ArrayList<PaymentTypeDetail> l_list = new ArrayList<PaymentTypeDetail>();
			paymentTypeDAO = RemittanceDAOFactory.getPaymentTypeDAO(getConnection());
			l_list = paymentTypeDAO.findPaymentTypeDetails(paymentTypeId);
			return l_list;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	//MBC End		
}
